package com.info.test.jiami;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

public class MyEncryption {
	private static int start= 20;
	private static int size= 100;
	private static int stlen= 30;
	public static void main(String[] args) {
		/* 源文件 */
		String path = "F:\\test2\\1.png";
		/* 加密文件 */
		String path2 = "F:\\test2\\2.png";
		/* 解密文件 */
		String path3 = "F:\\test2\\3.png";
		File file = new File(path);
		File file2 = new File(path2);
		File file3 = new File(path3);
		/* 文件加密 */
//		Encry(file, file2);
		/* 文件解密 */
		Decrypt(file2,file3);
	}
	/**
	 * 文件解密
	 * @param f1
	 * @param f2
	 */
	private static void Decrypt(File f1, File f2) {
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		int count = 0;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		byte[] b = new byte[size];
		int len = 0;
		try {
			fis = new FileInputStream(f1);
			bis = new BufferedInputStream(fis);
			
			fos = new FileOutputStream(f2);
			bos = new BufferedOutputStream(fos);
			while((len = bis.read(b))!=-1){
				System.out.println(count + "--->" + len + "--->" + Arrays.toString(b));
				if(len==size) {
					bos.write(b,start,stlen);
				}else {
					bos.write(b,0,len);
				}
				count++;
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (bis != null) {
					bis.close();
					bis = null;
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (bos != null) {
					bos.close();
					bos = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	/**
	 * 文件加密
	 * @param f1
	 * @param f2
	 */
	private static void Encry(File f1, File f2) {
		// TODO Auto-generated method stub
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		int count = 0;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		byte[] b = new byte[size];
		int len = 0;
		try {
			fis = new FileInputStream(f1);
			bis = new BufferedInputStream(fis);
			
			fos = new FileOutputStream(f2);
			bos = new BufferedOutputStream(fos);
			while ((len = bis.read(b, start, stlen)) != -1) {
				System.out.println(count + "--->" + len + "--->" + Arrays.toString(b));
				for (int i = 0; i < start; i++) {
					Random random = new Random();
					b[i]= (byte) (random.nextInt(256)-128);
				}
				for (int i = len+start+1; i < size; i++) {
					Random random = new Random();
					b[i]= (byte) (random.nextInt(256)-128);
				}
				if (len == stlen) {
					bos.write(b, 0, size);
				} else {
					bos.write(b, start, len);
				}
				count++;
			}
			bos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bis != null) {
					bis.close();
					bis = null;
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (bos != null) {
					bos.close();
					bos = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
